package request

import (
	"fox/common"
	"fox/models"
	"github.com/go-playground/validator/v10"
	"reflect"
	"regexp"
)

type User struct {
	Paging
	UserName string `json:"userName"` //账号
}

type SaveUser struct {
	UserName string `json:"username" binding:"required,min=5,max=10,alphanum"`
	NickName string `json:"nickname" binding:"required,min=5,max=10"`
	Password string `json:"password" binding:"required,min=6,max=10"`
}

type LoginUser struct {
	UserName string `json:"username" binding:"required,min=5,max=10,alphanum"`
	Password string `json:"password" binding:"required,min=6,max=10"`
}

type UpdateUser struct {
	ID       int64  `json:"id" binding:"required"`
	NickName string `json:"nickname" binding:"omitempty,min=5,max=10"`
	Avatar   string `json:"avatar" binding:"omitempty,uri"`
	Email    string `json:"email" binding:"omitempty,email"`
	Mobile   string `json:"mobile" binding:"omitempty,MobileValida"`
	Password string `json:"password" binding:"omitempty,min=6,max=10"`
	Status   int    `json:"status" binding:"omitempty,min=-1,max=1"`
}

var MobileValida validator.Func = func(fl validator.FieldLevel) bool {
	var (
		isOk bool
	)

	isOk, _ = regexp.MatchString("^1[23456789]\\d{9}$", fl.Field().String())
	if isOk {
		return true
	}
	return false
}

// ValidateUserSortFile 检验字段是否在结构体中
func ValidateUserSortFile(filed string) bool {
	var (
		user *models.User
		ele  reflect.Type
	)

	user = &models.User{}
	ele = reflect.TypeOf(user).Elem()
	for i := 0; i < ele.NumField(); i++ {
		var tagSetting = common.ParseTagSetting(ele.Field(i).Tag.Get("gorm"), ";")
		if tagSetting["COLUMN"] == filed {
			return true
		}
	}
	return false
}
